const webpack = require('webpack')
const { merge } = require('webpack-merge')  //webpack配置合并
const common = require('./webpack.common.js')
const chalk = require('chalk')  //编译环境控制台输出样式
const ip = require('ip')  //ip模块 ip.address()获取当前网络ip地址

let port = '9527'
let subPath = '/react-admin-template'  //二级目录，example: '/subpath'
let dirPath = subPath ? subPath + '/' : '/'
console.log(chalk.blue(`> 本地访问: http://localhost:${ port }${ dirPath }`))
console.log(chalk.blue(`> 局域网访问: http://${ ip.address() }:${ port }${ dirPath }`))

module.exports = merge(common, {
    mode: 'development',
    output: {
        publicPath: `${ dirPath }`
    },
    devtool: 'cheap-module-eval-source-map',
    devServer: {
        open: `http://localhost:${ port }${ dirPath }`,  //自动打开浏览器页面地址
        host: '0.0.0.0',  //开启局域网访问
        hot: true,
        port,
        proxy: {
            '/api': {
                target: 'http://42.193.144.245:3000',
                changeOrigin: true,
                pathRewrite: { '^/api': '' }
            }
        },
        historyApiFallback: {
            index: `${ dirPath }index.html`
        }
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.BASE_URL': JSON.stringify('/api')
        }),
        new webpack.HotModuleReplacementPlugin()  //模块热替换
    ]
})